//
//  RetrivePasswordViewController.m
//  NewApp
//
//  Created by  on 12-6-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RetrivePasswordViewController.h"
#import "NAAccount.h"
#import "PathHelper.h"
#import "NAError.h"
#import "SystemInfoViewController.h"
#import "ChecksEndViewController.h"

@interface RetrivePasswordViewController ()

/**
 *  Find password request did finished
 */
- (void) findPasswordRequestDidFinished:(RequestEngine *) engine withObject:(id) object;

/**
 *  Go home page
 */
- (void) goSystemInfoPageWithInfo:(NSString *) info;

@end

@implementation RetrivePasswordViewController

@synthesize scrollView,mailAddressTextfield;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.engine findPasswordInfos];
    
    self.scrollView.contentSize = CGSizeMake(320.f, 460.f);
    self.scrollView.showsVerticalScrollIndicator = NO;
}

- (void) goSystemInfoPageWithInfo:(NSString *) info {
    SystemInfoViewController *infoViewCtrl = [[SystemInfoViewController alloc] initWithSystemInfo:info];
    [self.navigationController pushViewController:infoViewCtrl animated:YES];
    [infoViewCtrl release];
}

- (IBAction) findPasswordButtonPressed:(id)sender {
    if (self.mailAddressTextfield.text.length  == 0) {
        [NAError showAlertMessageWithTitle:@"" 
                                   message:@"請輸入信箱"];
        return;
    }
    
    if (findPasswordReq != nil) 
        return;
    
    findPasswordReq = [[RequestEngine alloc] initWithTarget:self action:@selector(findPasswordRequestDidFinished:withObject:)];
    NSLog(@"==========%@",self.mailAddressTextfield.text);
    [findPasswordReq retrievePasswordWithMail:self.mailAddressTextfield.text];

}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        NSString *title = [dict objectForKey:@"top_title"];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
        
        [dict release];
        
    }
}

- (void) findPasswordRequestDidFinished:(RequestEngine *) engine withObject:(id) object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dict = (NSDictionary *) object;
        [dict retain];
        
        /*
        if ([[dict objectForKey:@"status"] isEqualToString:@"1"]) {
            [self goSystemInfoPageWithInfo:[dict objectForKey:@"msg"]];
        }else {
            [NAError showAlertMessageWithTitle:@"" message:[dict objectForKey:@"msg"]];
        }
        */

        NSString *msg = [dict objectForKey:@"msg"];
        // NSString *order_id = [result objectForKey:@"order_id"];
        
            
            ChecksEndViewController *vctrl = [[ChecksEndViewController alloc] init];
            vctrl.titleText = [dict objectForKey:@"top_title"];
            vctrl.msg = [msg retain];
        self.title = @"返回";
            [self.navigationController pushViewController:vctrl animated:YES];
            [vctrl release];
    }
    else {
        [[NAError errorWithCode:NAError_REQ_FAIL] showErrorMessage:nil];
    }
    
    NA_RELEASE_SAFELY(findPasswordReq);
}

#pragma mark -
#pragma mark - UITextfield delegate methods

- (BOOL) textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    return YES;
}



@end
